using Cinemachine;
using DG.Tweening;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class WorldManager
{
    public static WorldManager Instance = new WorldManager();
    public float worldSize { get { return m_GenerateSize; } }

    bool m_IsInited = false;

    List<QuiQuiObj> m_QuiQuis = new List<QuiQuiObj>();
    float m_GenerateSize = 45;

    CinemachineVirtualCamera m_CinemachineCamera;
    QuiQuiObj m_PlayerQuiQui;
    WorldManager()
    {

    }

    public void Init()
    {
        if (m_IsInited)
            return;
        m_IsInited = true;
        m_CinemachineCamera = GameObject.FindObjectOfType<CinemachineVirtualCamera>();

        for (int i = 0; i < 35; ++i)
        {
            var quiqui = CreateQuiQui();
            quiqui.RandomLife();
        }
        var playerQuiqui = GetHealthyQuiQui();
        if (!playerQuiqui)
            playerQuiqui = CreateQuiQui();
        ConvertToPlayer(playerQuiqui);
#if UNITY_ANDROID
        UIManager.Instance.OpenUI(UIManager.UIEnum.AndroidOperationUI);
#endif
    }

    public QuiQuiObj CreateQuiQui()
    {
        var quiqui = Pool.GetInstance().GetObj("Prefabs/QuiQui");
        quiqui.transform.position = new Vector3(Random.Range(-m_GenerateSize / 2, m_GenerateSize / 2), Random.Range(-m_GenerateSize / 2, m_GenerateSize / 2), 0);

        quiqui.AddComponent<NPCController>();
        var quiquiCom = quiqui.GetComponent<QuiQuiObj>();
        quiquiCom.OnCreate();
        m_QuiQuis.Add(quiquiCom);
        return quiquiCom;
    }
    void ConvertToPlayer(QuiQuiObj quiqui)
    {
        m_PlayerQuiQui = quiqui;
        if (m_PlayerQuiQui.GetComponent<NPCController>())
            GameObject.Destroy(m_PlayerQuiQui.gameObject.GetComponent<NPCController>());
        m_PlayerQuiQui.AddComponent<PlayerControle>();
        m_CinemachineCamera.Follow = m_PlayerQuiQui.transform;
        m_CinemachineCamera.LookAt = m_PlayerQuiQui.transform;
    }
    public QuiQuiObj GetHealthyQuiQui()
    {
        foreach (var i in m_QuiQuis)
        {
            if (i.gameObject.activeSelf && i.hungryVaue > 10 && i.lifeScale < 0.5f)
            {
                return i;
            }
        }
        return null;
    }

    public void CycleQuiQui(GameObject quiqui, QuiQuiObj.DieWay dieWay)
    {
        if (quiqui.GetComponent<PlayerControle>())
        {
            var diePanel = UIManager.Instance.OpenUI(UIManager.UIEnum.DiePanel) as DiePanel;
            switch (dieWay)
            {
                case QuiQuiObj.DieWay.Hungry:
                    diePanel.SetText("����");
                    break;
                case QuiQuiObj.DieWay.Age:
                    diePanel.SetText("����");
                    break;
                case QuiQuiObj.DieWay.Kill:
                    diePanel.SetText("ɱ��");
                    break;
                case QuiQuiObj.DieWay.Tired:
                    diePanel.SetText("����");
                    break;
            }
            GameObject.Destroy(quiqui.GetComponent<PlayerControle>());
        }
        var quiquiCom = quiqui.GetComponent<QuiQuiObj>();
        if (quiquiCom && quiquiCom.takingObj)
            quiquiCom.DoPlace();

        var npcController = quiqui.GetComponent<NPCController>();
        if (npcController)
            GameObject.Destroy(npcController);
        var foodObj = IntersectionObjManager.Instance.CreateObj(IntersectionObjManager.IntersectionObjType.BigFood).GetComponent<FoodObj>();
        foodObj.SetColliderEnabled(false);
        foodObj.transform.position = quiqui.transform.position;
        foodObj.transform.DOMove(quiqui.transform.position + new Vector3(Random.Range(-1, 1f), Random.Range(-1, 1f), 0), 0.5f);
        DOVirtual.DelayedCall(1, () =>
        {
            foodObj.SetColliderEnabled(true);
        });
        Pool.GetInstance().DestroyObj(quiqui);
    }
    public void CreatePlayer()
    {
        var playerQuiqui = m_QuiQuis[Random.Range(0, m_QuiQuis.Count)];
        var healthyQuiQui = GetHealthyQuiQui();
        if (!healthyQuiQui)
            healthyQuiQui = CreateQuiQui();
        ConvertToPlayer(healthyQuiQui);
    }
    public bool isGlobalView { get { return m_IsGlobalView; } }
    bool m_IsGlobalView = false;
    public void SetGlobalView(bool global)
    {
        m_IsGlobalView = global;
        var newValue = global ? 15 : 5;
        DOTween.To(() => m_CinemachineCamera.m_Lens.OrthographicSize, x => m_CinemachineCamera.m_Lens.OrthographicSize = x, newValue, 0.5f);
    }
    public void Update()
    {

    }
    public QuiQuiObj GetPlayerQuiQui()
    {
        return m_PlayerQuiQui;
    }
    public PlayerControle GetPlayerControle()
    {
        if (!m_PlayerQuiQui)
            return null;
        var playerController = m_PlayerQuiQui.GetComponent<PlayerControle>();
        return playerController;
    }
}
